﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WB.Library;
using System.Collections.Generic;
public partial class Pager : System.Web.UI.UserControl
{
    class PaggingLink
    {
        public int Index { get; set; }
        public string Css { get; set; }
    }
    public delegate void PaggingHandler(object sender, PaggingEventArgs e);
    public event PaggingHandler ChangePage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    #region Property
    /// <summary>
    /// View from page
    /// </summary>
    protected int StartPage
    {
        get
        {
            if (CurrentPageIndex - NumPageView / 2 > 0)
                return CurrentPageIndex - NumPageView / 2;
            return 0;
        }
    }

    /// <summary>
    /// View to page
    /// </summary>
    protected int EndPage
    {
        get
        {
            if (CurrentPageIndex + NumPageView / 2 < TotalPage)
                return CurrentPageIndex + NumPageView / 2;
            return TotalPage;
        }
        //return StartPage + NumPageView < TotalPage ? StartPage + NumPageView : TotalPage; }
    }



    /// <summary>
    /// Number of page view on screen
    /// </summary>
    public int NumPageView
    {
        get
        {
            if (this.ViewState["NumPageView"] == null)
            {
                NumPageView = 7;
            }
            return (int)this.ViewState["NumPageView"];
        }
        set
        {
            this.ViewState["NumPageView"] = value;
        }
    }
    /// <summary>
    /// Number record in one page
    /// </summary>
    public int PageSize
    {
        get
        {
            if (this.ViewState["PageSize"] == null)
                PageSize = 20;
            return (int)this.ViewState["PageSize"];
        }
        set
        {
            //if (value <= 0)
            //    throw new Exception("Page size > 0.");
            this.ViewState["PageSize"] = value;
        }
    }
    /// <summary>
    /// CSS of selected page
    /// </summary>
    public string CurrentPageCSS
    {
        get
        {
            if (this.ViewState["CurrentPageCSS"] == null)
                CurrentPageCSS = "CurrentPage";
            return (string)this.ViewState["CurrentPageCSS"];
        }
        set { this.ViewState["CurrentPageCSS"] = value; }
    }
    /// <summary>
    /// CSS of page navigation link
    /// </summary>
    public string PageIndexCSS
    {
        get
        {
            if (this.ViewState["PageIndexCSS"] == null)
                PageIndexCSS = "page";
            return (string)this.ViewState["PageIndexCSS"];
        }
        set { this.ViewState["PageIndexCSS"] = value; }
    }

    /// <summary>
    /// Total record of datasource
    /// </summary>
    public int TotalRecord
    {
        get
        {
            if (this.ViewState["TotalRecord"] == null)
                TotalRecord = 0;
            return (int)this.ViewState["TotalRecord"];
        }
        set { this.ViewState["TotalRecord"] = value; }
    }
    /// <summary>
    /// Total page of datasource
    /// </summary>
    public int TotalPage
    {
        get
        {
            return (int)Math.Ceiling(1.0 * TotalRecord / PageSize);
        }
    }
    /// <summary>
    /// Current Page
    /// </summary>
    public int CurrentPageIndex
    {
        get
        {
            if (IsPostBackPage == false)
                return 1;
            if (this.ViewState["CurrentPage"] == null)
                CurrentPageIndex = 1;
            return (int)this.ViewState["CurrentPage"];
        }
        set { this.ViewState["CurrentPage"] = value; }
    }
    #endregion

    public void LoadPageLink(PagedData pagging)
    {
        //CurrentPageIndex = pagging.PageCurrent;
        PageSize = pagging.PageSize;
        TotalRecord = pagging.TotalRows;
        

        phdPrev.Visible = CurrentPageIndex > 1;
        phdNext.Visible = CurrentPageIndex < TotalPage;

        if (TotalPage <= 1)
        {
            this.Visible = false;
            return;
        }

       // int cnt = 0;
        List<PaggingLink> links;
        bool isleft = false, isright = false;
        repLinkLeft.DataSource = null;
        // Thể hiện phan trang khúc đầu 1 2 ...
        if (CurrentPageIndex >= NumPageView - 3)
        {
            links = new List<PaggingLink>();
            isleft = true;
            for (int i = 1; i <= 2; i++)
                links.Add(new PaggingLink { Index = i });
            repLinkLeft.DataSource = links;
        }
        repLinkLeft.DataBind();

        // Thể hiện phân trang khúc sau ... 9 10
        repLinkRight.DataSource = null;
        if (CurrentPageIndex <= (TotalPage - NumPageView + 3))
        {
            links = new List<PaggingLink>();
            isright = true;
            for (int i = TotalPage - 1; i <= TotalPage; i++)
                links.Add(new PaggingLink { Index = i });
            repLinkRight.DataSource = links;
            repLinkRight.DataBind();
        }
        repLinkRight.DataBind();

        links = new List<PaggingLink>();
        repLinkCenter.DataSource = null;
        if (isleft && isright)
        {
            // phân trang khúc giữa ... 5 6 7 ...
            for (int i = CurrentPageIndex - 2; i <= CurrentPageIndex + 2; i++)
            {
                if (i > 2 + 1 && i < TotalPage - 2)
                {
                    if (i == CurrentPageIndex)
                        links.Add(new PaggingLink { Index = i, Css = "current" });
                    else
                        links.Add(new PaggingLink { Index = i });
                }
            }
        }
        else if (isleft && !isright)
        {
            // phân trang còn lại
            for (int i = TotalPage - NumPageView; i <= TotalPage; i++)
            {
                if (i > 2 + 1)
                {
                    if (i == CurrentPageIndex)
                        links.Add(new PaggingLink { Index = i, Css = "current" });
                    else
                        links.Add(new PaggingLink { Index = i });
                }
            }
        }
        else if (!isleft && isright)
        {
            // phân trang đầu tiên
            for (int i = 1; i <= NumPageView; i++)
            {
                if (i < TotalPage - 2 - 1)
                {
                    if (i == CurrentPageIndex)
                        links.Add(new PaggingLink { Index = i, Css = "current" });
                    else
                        links.Add(new PaggingLink { Index = i });
                }
            }
        }
        else
        {
            for (int i = 1; i <= TotalPage; i++)
                if (i == CurrentPageIndex)
                    links.Add(new PaggingLink { Index = i, Css = "current" });
                else
                    links.Add(new PaggingLink { Index = i });
        }
        repLinkCenter.DataSource = links;
        repLinkCenter.DataBind();

        IsPostBackPage = false;
    }

    protected void CallChangeEvent(PaggingEventArgs e)
    {
        ChangePage(this, e);
        IsPostBackPage = true;
    }

    protected void repLink_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        CurrentPageIndex = int.Parse(e.CommandArgument.ToString());
        CallChangeEvent(new PaggingEventArgs { PageCurrent = CurrentPageIndex });
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (CurrentPageIndex < TotalPage)
        {
            CurrentPageIndex = CurrentPageIndex + 1;
            CallChangeEvent(new PaggingEventArgs { PageCurrent = CurrentPageIndex });
        }
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        if (CurrentPageIndex > 0)
        {
            CurrentPageIndex = CurrentPageIndex - 1;
            CallChangeEvent(new PaggingEventArgs { PageCurrent = CurrentPageIndex });
        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPageIndex = 0;
        CallChangeEvent(new PaggingEventArgs { PageCurrent = CurrentPageIndex });
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        CurrentPageIndex = TotalPage - 1;
        CallChangeEvent(new PaggingEventArgs { PageCurrent = CurrentPageIndex });
    }

    protected bool IsPostBackPage
    {
        get
        {
            return true;
            //if (this.ViewState["IsPostBackPage"] == null)
            //    IsPostBackPage = false;
            //return (bool)this.ViewState["IsPostBackPage"];
        }

        set { this.ViewState["IsPostBackPage"] = value; }
    }
}
